﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;
using System.Web.SessionState;
using System.Xml;
using Axis.Dao.News;
using Axis.BusinessObject.News;
using System.Text;
namespace Axis.Web.Administrator.Views.Static.Service
{
    /// <summary>
    /// Summary description for StaticGrid
    /// </summary>
    public class StaticGrid : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string output = GetNewsList(context);
            context.Response.Write(output);
        }
     
        protected string GetNewsList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            CNewsDao core = new CNewsDao();
            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];

            CCore cCore = new CCore();
            string inputValue = "<InputValue CategoryID='75' Context='NEWSSTATITIC'/>";
            IList<CNews> list = core.GetNews(inputValue);
            int totalRecords = core.LastOutputValue.TotalRows;
            int totalPages = core.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CNews> projects, int numberOfRows, int pageIndex, int totalPages)
        {
            if (projects != null && projects.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CNews project in projects)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = project.ID;
                    row.cell = new string[23];
                    row.cell[0] = project.ID.ToString();
                    row.cell[1] = project.Code;
                    row.cell[2] = project.Title;
                    row.cell[3] = project.CategoryID.ToString();
                    row.cell[4] = project.Content;
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        protected string GetDataRequired(CProject project)
        {
            string ret = string.Empty;
            if (project.MobileRequired)
                ret += "1,";
            if (project.GenderRequired)
                ret += "2,";
            if (project.ClassRequired)
                ret += "3,";
            if (project.HouseholdIncomeRequired)
                ret += "4,";
            if (project.EducationRequired)
                ret += "5,";
            if (project.JobIDRequired)
                ret += "6,";
            if (project.MaritalStatusRequired)
                ret += "7,";
            if (project.ProvinceIDRequired)
                ret += "8,";
            if (project.CMNDRequired)
                ret += "9,";
            return ret;
        }
        public string GetInputValue(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            root.AppendChild(InputValue);
            InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            InputValue.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());

            StringBuilder qsXml = new StringBuilder();
            string[] allKeys = request.Form.AllKeys;
            if (allKeys.Length == 0)
            {
                allKeys = request.QueryString.AllKeys;
            }
            foreach (string pKey in allKeys)
            {
                string key = pKey.Trim();
                string value = request[key].Trim();

                if (key.IndexOf("Gift_") != -1)
                {
                    XmlElement gift = doc.CreateElement("Gift");
                    string giftID = key.Replace("Gift_", "");
                    gift.SetAttribute("GiftID", giftID);
                    gift.SetAttribute("Quantity", value);
                    root.AppendChild(gift);
                }
                else
                {
                    InputValue.SetAttribute(key, value);
                }
            }
            return root.InnerXml;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}